package com.client.qiuqiu;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

import androidx.annotation.Nullable;

import java.io.IOException;
import java.io.PrintStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

public class MessageService extends Service {
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    //消息接收服务
    @Override
    public void onCreate() {
        super.onCreate();
        MessageService.Connect_Thread Connect_thread = new MessageService.Connect_Thread();
        Connect_thread.start();
        try {
            Connect_thread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        //开始接收消息
        MessageService.receiverMessage_Thread receiverMessage_thread = new receiverMessage_Thread();
        receiverMessage_thread.start();
    }

    class Connect_Thread extends Thread{
        public void run(){
            User user = ((User)getApplicationContext());
            //在服务端ipconfig，拿到ip填入
            SocketAddress addr = new InetSocketAddress(user.serverIP, 80);
            Socket socket = new Socket();
            {
                try {
                    socket.connect(addr, 1000);
                    //socket.setSoTimeout(1000);
                } catch (IOException e) {
                    e.printStackTrace();
                    System.exit(0);
                }
            }

            //记录socket
            user.setMsgSocket(socket);

            //获取服务器端的输出流
            PrintStream printStream = null;
            try {
                printStream = new PrintStream(user.getMsgSocket().getOutputStream());
            } catch (IOException e) {
                e.printStackTrace();
            }

            //发送socket类型
            //客户端与服务器通讯socket
            printStream.println("2");
            printStream.println(user.getId());
        }

    }

    class receiverMessage_Thread extends Thread{
        public void run(){
            User user = ((User)getApplicationContext());
            Socket messageSocket = user.getMsgSocket();

            //获取输入流
            Scanner scanner = null;
            try {
                scanner = new Scanner(messageSocket.getInputStream());
                while (scanner.hasNextLine()) {
                    //处理客户端输入的字符串
                    String senderID = null;
                    Chatmessage chatmessage = new Chatmessage();
                    senderID = scanner.nextLine();
                    chatmessage.setSenderID(senderID);
                    chatmessage.setReceiverID(user.getId());
                    chatmessage.setMessage(scanner.nextLine());
                    chatmessage.setDateObject(new Date());
                    if(user.getChatMessagesBox().get(senderID) == null){
                        //之前无消息
                        List<Chatmessage> chatmessages = new ArrayList<>();
                        chatmessages.add(chatmessage);
                        user.getChatMessagesBox().put(senderID, chatmessages);
                    }else {
                        //有消息
                        List<Chatmessage> chatmessages = user.getChatMessagesBox().get(senderID);
                        chatmessages.add(chatmessage);
                        user.getChatMessagesBox().put(senderID, chatmessages);
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
